﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;
using System.Xml;
using System.Text;
using Touch2Like.Web;
using Touch2Like.DAL;
using Touch2Like.Model;
using Touch2Like.SharedLibrary;

namespace Touch2Like.Web
{
    public partial class Validate : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Cache.SetCacheability(HttpCacheability.NoCache);
            Response.ExpiresAbsolute = DateTime.Now.AddDays(-1d);
            Response.Expires = -1500;
            Response.CacheControl = "no-cache";


            bool authenticated = WebUtility.Authentication.CheckBasicAuthentication(this, Utility.WCFUsername, Utility.WCFPassword);
            if (!authenticated)
                return;

            this.Page.Response.ContentType = "text/xml";
            //Read XML posted via HTTP
            StreamReader reader = new StreamReader(this.Page.Request.InputStream);
            String Data = reader.ReadToEnd();

            string[] elements = parseRequestXML(Data);

            UserTagDAL tag = new UserTagDAL();
            UserTagInfo tagInfo = new UserTagInfo();
            tagInfo.TagUID = elements[0].ToString();
            tagInfo.TagContent = elements[1].ToString();
            //tag.(tagInfo);
        }

        public string[] parseRequestXML(string xmlString)
        {
            string[] elements = new String[3];

            XmlDocument xmlRequest = new XmlDocument();
            xmlRequest.InnerXml = xmlString;

            XmlNamespaceManager xnm = new XmlNamespaceManager(xmlRequest.NameTable);

            XmlNode requestNode = xmlRequest.SelectSingleNode("Touch2Like", xnm);

            elements[0] = Utility.GetNodeContentByXPath(requestNode, "TagUID", xnm);
            elements[1] = Utility.GetNodeContentByXPath(requestNode, "Content", xnm);

            return elements;
        }
    }
}